package xyz.syyrjx.registry;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.syyrjx.client.SyyrjxRpcClient;
import xyz.syyrjx.initializer.SyyrjxRpcChannelInitializer;
import xyz.syyrjx.registry.conf.SyyrjxRpcRegistryConfiguration;
import xyz.syyrjx.registry.handler.SyyrjxRpcRegistryClientHandler;
import xyz.syyrjx.registry.handler.SyyrjxRpcRegistryServerHandler;
import xyz.syyrjx.registry.task.SyyrjxRpcRegistrySynchronizeTask;
import xyz.syyrjx.registry.task.SyyrjxRpcRegistryReconncetSchedule;
import xyz.syyrjx.server.SyyrjxRpcServer;

import java.util.*;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/11 - 14:30
 */
@Data
public class SyyrjxRpcRegistryClass {

    private final static Logger LOGGER = LoggerFactory.getLogger(SyyrjxRpcRegistryClass.class);

    public void serverStart() {
        SyyrjxRpcServer registryServer = new SyyrjxRpcServer();
        registryServer.setPort(SyyrjxRpcRegistryConfiguration.getConfiguration().getPort());

        SyyrjxRpcChannelInitializer channelInitializer = new SyyrjxRpcChannelInitializer();
        channelInitializer.setMaxReadSize(SyyrjxRpcRegistryConfiguration.getConfiguration().getMaxReadSize());
        channelInitializer.setHandler(new SyyrjxRpcRegistryServerHandler());

        registryServer.run(channelInitializer);
    }

    public void clientStart() {

        String[] registryAddress = SyyrjxRpcRegistryConfiguration.getConfiguration().getRegistryAddress();
        if (registryAddress.length <= 1) {
            return;
        }


        for (String address : registryAddress) {
            String[] s = address.split(":");
            String host = s[0];
            int port = Integer.parseInt(s[1]);

            //不连接自己
            if (Objects.equals(SyyrjxRpcRegistryConfiguration.getConfiguration().getHost(), host)
            && SyyrjxRpcRegistryConfiguration.getConfiguration().getPort() == port) {
                LOGGER.debug("不需要连接自己");
                continue;
            }

            SyyrjxRpcClient client = new SyyrjxRpcClient();
            client.setRemoteHost(host);
            client.setRemotePort(port);

            SyyrjxRpcChannelInitializer channelInitializer = new SyyrjxRpcChannelInitializer();
            SyyrjxRpcRegistryClientHandler handler = new SyyrjxRpcRegistryClientHandler();
            channelInitializer.setMaxReadSize(SyyrjxRpcRegistryConfiguration.getConfiguration().getMaxReadSize());
            channelInitializer.setHandler(handler);

            try {
                client.run(channelInitializer);
                SyyrjxRpcRegistrySynchronizeTask.addClient(client);
            }catch (Exception e){
                LOGGER.error(client + "客户端启动失败");
                SyyrjxRpcRegistryReconncetSchedule.addClient(client);
            }
        }
        //有连接失败的就重试
        if (SyyrjxRpcRegistryReconncetSchedule.countFailClient() != 0) {
            SyyrjxRpcRegistryReconncetSchedule.reconncetStart();
            LOGGER.debug("启动重连接任务");
        }
    }
}
